package com.e3.bill.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.web.core.model.SuperModel;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyButton;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.CardLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;
/**
 * 用CardLayout进行card界面和list界面的切换。
 * 当双击master grid的记录或点击修改按钮时切换到card界面进行编辑。
 * 此时save按钮可用。
 * Description: <br>
 * @author: <a href="mailto:senton1101@126.com">TianXiangdong</a> 2010-9-20
 */
public abstract class MyBillUI<D extends SuperModel,M extends SuperModel> extends LayoutContainer {
	
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	private BillListPanel<D, M> billListPanel;
	
	private BillCardPanel<D, M> billCardPanel;
	
	private CardLayout cardLayout = new CardLayout();
	
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		this.setLayout(new FitLayout());
		
		ContentPanel contentPanel = new ContentPanel();
		initContentPanel(contentPanel);
		
		ToolBar toolbar = new ToolBar();
		initToolBar(toolbar);
		
		contentPanel.setTopComponent(toolbar);
		add(contentPanel);
	}

	protected void initContentPanel(ContentPanel contentPanel){
		contentPanel.setLayout(cardLayout);
		contentPanel.setHeaderVisible(false);
		contentPanel.add(getBillListPanel());
		contentPanel.add(getBillCardPanel());
		
		
	}
	
	public BillListPanel<D, M> getBillListPanel() {
		if(billListPanel == null){
			billListPanel = createBillListPanel();
		}
		return billListPanel;
	}
	
	public BillCardPanel<D, M> getBillCardPanel() {
		if(billCardPanel == null){
			billCardPanel = createBillCardPanel();
		}
		return billCardPanel;
	}

	protected void initToolBar(ToolBar toolbar){
		MyButton[] buttons = getToolButtons();
		if(buttons!=null){
			for (MyButton myButton : buttons) {
				toolbar.add(myButton);
			}
		}
		BillStateMachine.INSTANCE.onBillList();
	}
	
	protected MyButton[] getToolButtons(){
		BaseAction[] actions = {BillStateMachine.QUERY_ACTION,BillStateMachine.NEW_ACTION,BillStateMachine.MODIFY_ACTION
				,BillStateMachine.DELETE_ACTION,BillStateMachine.SAVE_ACTION
				,BillStateMachine.ACTION_APPROVE_PASS,BillStateMachine.ACTION_APPROVE_NOPASS
				,BillStateMachine.CANCEL_ACTION,BillStateMachine.ACTION_GOTO_LIST
		};
		List<MyButton> buttons = new ArrayList<MyButton>();
		Menu menu = getRefNewMenu();
		if(menu!=null){
			MyButton refButton = new MyButton(BillStateMachine.ACTION_NEW_REF);
			refButton.setMenu(menu);
			buttons.add(refButton);
		}
		for (BaseAction action : actions) {
			MyButton button = new MyButton(action);
			button.addSelectionListener(btnSelectListener);
			buttons.add(button);
		}
		
		MyButton[] btns = new MyButton[buttons.size()];
		buttons.toArray(btns);
		return btns;
	}
	

	protected void onBtnMnuEvent(BaseAction action) {
		if(action == BillStateMachine.ACTION_GOTO_LIST){
			cardLayout.setActiveItem(getBillListPanel());
			BillStateMachine.INSTANCE.onBillList();
		}
		else if(action == BillStateMachine.MODIFY_ACTION){
			cardLayout.setActiveItem(getBillCardPanel());
			BillStateMachine.INSTANCE.onBillCard();
		}
		else if(action == BillStateMachine.NEW_ACTION){
			cardLayout.setActiveItem(getBillCardPanel());
			BillStateMachine.INSTANCE.onBillCard();
		}
	}
	
	protected Menu getRefNewMenu(){
		return null;
	}
	
	protected abstract BillListPanel<D, M> createBillListPanel();

	protected abstract BillCardPanel<D, M> createBillCardPanel();

}
